package cn.b504.qpcr.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.FilterType
import org.springframework.web.servlet.ViewResolver
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
import org.springframework.web.servlet.view.InternalResourceViewResolver
import org.springframework.web.servlet.view.JstlView

@Configuration
@ComponentScan(basePackages = ["cn.b504.qpcr"],
    excludeFilters = [ComponentScan.Filter(type = FilterType.ANNOTATION, value = [EnableWebMvc::class])])
open class ApplicationContextXml


@Configuration
@EnableWebMvc
@ComponentScan("cn.b504.qpcr.controller")
open class DispatcherServletXml : WebMvcConfigurer {

  @Bean
  open fun viewResolver(): ViewResolver {
    val resolver = InternalResourceViewResolver()
    resolver.setViewClass(JstlView::class.java)
    resolver.setPrefix("/WEB-INF/WebContent/")
    resolver.setSuffix(".jsp")
    resolver.setExposeContextBeansAsAttributes(true)
    return resolver
  }

  override fun configureDefaultServletHandling(configurer: DefaultServletHandlerConfigurer) {
    configurer.enable()
  }

  override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
    registry.addResourceHandler("/**").addResourceLocations("/WEB-INF/WebContent/")
  }
}


class WebXml : AbstractAnnotationConfigDispatcherServletInitializer() {

  override fun getRootConfigClasses(): Array<Class<*>> {
    return arrayOf(ApplicationContextXml::class.java)
  }

  override fun getServletConfigClasses(): Array<Class<*>> {
    return arrayOf(DispatcherServletXml::class.java)
  }

  override fun getServletMappings(): Array<String> {
    return arrayOf("/")
  }
}